package com.school.dcc.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;

/**
 * @author 又菜又爱玩
 * @version 1.0
 * @date 2021/12/11 19:13
 * @blog https://blog.csdn.net/biancheng4/article/details/121727982
 */
public class CodeGenerator {
    public static void main(String[] args) {
        //构建一个代码自动生成器
        AutoGenerator autoGenerator = new AutoGenerator();
        //配置策略


        //1.全局配置、
        GlobalConfig globalConfig = new GlobalConfig();
        String property = System.getProperty("user.dir");
        globalConfig.setOutputDir(property+"/src/main/java");
        globalConfig.setAuthor("又菜又爱玩");
        globalConfig.setOpen(false);
        globalConfig.setFileOverride(false);
        globalConfig.setServiceName("%sService");
        globalConfig.setIdType(IdType.AUTO);
        globalConfig.setDateType(DateType.ONLY_DATE);
        globalConfig.setSwagger2(true);
        autoGenerator.setGlobalConfig(globalConfig);


        //2、设置数据源
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("root");
        dsc.setDbType(DbType.MYSQL);
        autoGenerator.setDataSource(dsc);



        //3、包配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName("study");
        pc.setParent("com.school");
        pc.setEntity("pojo");
        pc.setMapper("mapper");
        pc.setService("service");
        pc.setController("controller");

        autoGenerator.setPackageInfo(pc);


        //4、策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setInclude("news"); //要映射到表明
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        strategy.setLogicDeleteFieldName("deleted");
        strategy.setVersionFieldName("version");
        TableFill creat = new TableFill("creatTime", FieldFill.INSERT);
        TableFill update = new TableFill("updateTime", FieldFill.INSERT_UPDATE);
        strategy.setRestControllerStyle(true);
        strategy.setControllerMappingHyphenStyle(true);
        ArrayList<TableFill> TableFill = new ArrayList<>();
        TableFill.add(creat);
        TableFill.add(update);
        strategy.setTableFillList(TableFill);
        autoGenerator.setStrategy(strategy);

        autoGenerator.execute();
    }
}
